package org.neframework.webtest.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.tmsps.neframework.core.common.page.Page;
import com.tmsps.neframework.jpa.base.BaseService;
import com.tmsps.neframework.jpa.sql.param.NeParamList;

public class RoleService extends BaseService {

	public List<Map<String, Object>> selectRole(String srh_uname, Map<String, String> sort_params, Page page) {
		String sql = "select * from t_fk_role t where t.status=0  order by t.name asc";

		NeParamList params = NeParamList.makeParams();
		params.addLike(srh_uname);

		List<Map<String, Object>> list = jcn.queryForList(sql, params, sort_params, page);
		List<Map<String, Object>> roles = new ArrayList<Map<String, Object>>();

		for (Map<String, Object> map : list) {
			String codes = (String) map.get("auth_codes");
			String[] code = codes.split(",");
			String value = "'0'";
			for (String string : code) {
				value += ",'" + string + "'";
			}
			String sql1 = "select GROUP_CONCAT(t.name) auth_codes from t_fk_auth t where t.status=0 and t.code in("
					+ value + ") ";
			Map<String, Object> obj = jc.queryForMap(sql1);
			map.put("auth_codes", obj.get("auth_codes"));
			roles.add(map);
		}

		return roles;
	}

	public List<Map<String, Object>> selectRoleList(String srh_uname, Map<String, String> sort_params, Page page) {
		String sql = "select * from t_fk_role t where t.status=0 and (t.name like ?) order by t.name desc";
		NeParamList params = NeParamList.makeParams();
		params.addLike(srh_uname);

		List<Map<String, Object>> list = jcn.queryForList(sql, params, sort_params, page);
		return list;
	}


	public static void main(String[] args) {
		String sql = "select * from t_fk_role t where t.status=0 and (t.name like ?) order by t.name desc";
		Integer position = sql.toLowerCase().indexOf("from ");
		int endPosition = sql.toLowerCase().indexOf(" order ");
		String sql_count = "select count(*) cnt " + sql.substring(position, endPosition);
		System.err.println(sql_count);
	}
}
